package com.zone.api.http

import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

/**
 * Author落幕
 * Time20:08
 * https 安全验证跳过
 */
object SSLSocketClient {
    //获取这个SSLSocketFactory
    fun getSSLSocketFactory(trustManager: TrustManager): SSLSocketFactory {
        try {
            // 创建 SSLContext 实例
            val sslContext = SSLContext.getInstance("TLS")
            // 初始化 SSLContext，传入信任管理器
            sslContext.init(null, arrayOf(trustManager), SecureRandom())
            // 返回 SSLSocketFactory
            return sslContext.socketFactory
        } catch (e: Exception) {
            throw RuntimeException("Failed to create SSL socket factory", e)
        }
    }

    //获取TrustManager
    fun getTrustManager(): Array<TrustManager> {
        return arrayOf(
            object : X509TrustManager {
                override fun checkClientTrusted(
                    chain: Array<X509Certificate>,
                    authType: String
                ) {
                }

                override fun checkServerTrusted(
                    chain: Array<X509Certificate>,
                    authType: String
                ) {
                }

                override fun getAcceptedIssuers(): Array<X509Certificate> {
                    return arrayOf()
                }
            }
        )
    }
}